
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using Photon.Pun.UtilityScripts;
using System.Collections.Generic;
using HashTable = ExitGames.Client.Photon.Hashtable;
public enum GameState
{
    //选角色阶段
    CharacterSelect,
    // 等待某一方更换角色，期间需要暂停player进程，为阵亡玩家打开选择角色面板，初始化角色位置...
    WaittingExchangeChar,
    // 游戏开始，进入主刷新回合
    Start,
    // 游戏中
    Gameing,
    // 结束
    Over
}
public class GameManager : MonoBehaviourPunCallbacks
{
    #region 垃圾代码
    //以前用的测试代码，不用看
    /*public GameObject readyButton;
    public GameObject outputButton;

    public GameObject InitCharacterCard;    //初始的3张角色卡
    public GameObject enemyCharacterCard;   //敌方的3张角色卡

    public GameObject PlayerCardsPool;
    public GameObject EnemyCardsPool;

    public void ReadyToPlay()
    {
        readyButton.SetActive(false);
        outputButton.SetActive(true);

        //for (int i = 0; i < 3; i++)
        //{
        //    InitCharacterCard = GameObject.Instantiate(InitCharacterCard, PlayerCardsPool.transform);
        //    InitCharacterCard.GetComponent<InitCharacterCards>().InitInfo(Random.Range(0, DataManager.Instance.characterCards.Count));
        //}
        
        //for (int i = 0; i < 3; i++)
        //{
        //    enemyCharacterCard = GameObject.Instantiate(enemyCharacterCard, EnemyCardsPool.transform);
        //}
        foreach (var item in DataManager.Instance.characterCards)
        {
            int select = Random.Range(0, 2);
            if (select == 0)
            {     //生成己方卡牌
                InitCharacterCard = GameObject.Instantiate(InitCharacterCard, PlayerCardsPool.transform);
                InitCharacterCard.GetComponent<InitCharacterCards>().InitInfo(item.Key);
            }
            else
                enemyCharacterCard = GameObject.Instantiate(enemyCharacterCard, EnemyCardsPool.transform);     //生成敌方卡牌
        }
    }

    public void PlayCard()
    {
        var player = PhotonNetwork.CurrentRoom.GetPlayer(1);
        if ((bool)player.CustomProperties["selected"])
        {
            PhotonNetwork.Destroy(this.GetComponent<PhotonView>());

        }
    }
    //public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
    //{
    //    if (targetPlayer == photonView.Owner)
    //    {
    //        PlayCard();
    //    }
    //}*/
    #endregion

    #region 单例模式
    public static GameManager Instance;
    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
            Destroy(gameObject);
        DontDestroyOnLoad(this);
    }
    #endregion


    private GameState gameState;
    public GameState GameState
    {
        get => gameState;
        set
        {
            gameState = value;
            switch (gameState)
            {
                case GameState.CharacterSelect:
                    CharacterSelectManager.Instance.EnterCharacterSelect();     // 进入选择角色场景后进行角色选择
                    break;
                case GameState.WaittingExchangeChar:

                    break;
                case GameState.Start:
                    // 进入游戏
                    RoundManager.Instance.EnterGame();
                    GameState = GameState.Gameing;
                    break;

                case GameState.Gameing:
                    break;

                case GameState.Over:
                    Debug.Log("游戏结束");
                    // 进入结算页面
                    GameOver();
                    break;
            }
        }
    }



    void Start()
    {
        GMTable.Add("Pool_CharCards", pool_CharCards);
        GameState = GameState.Start;    // TODO:记得改回select，因为该脚本首次出现与角色选择界面
    }

    /// <summary>
    /// 游戏结束，进入结算页面
    /// </summary>
    public void GameOver()
    {
        // 停止所有协程
        Player.Instance.StopAllCoroutines();
        Enemy.Instance.StopAllCoroutines();
        RoundManager.Instance.StopAllCoroutines();
    }

    #region 信息存储
    private List<CharacterCard> pool_CharCards;
    public List<CharacterCard> Pool_CharCards
    {
        get { return pool_CharCards; }
        set 
        { 
            pool_CharCards = value;
            GMTable["Pool_CharCards"] = pool_CharCards;
            PhotonNetwork.LocalPlayer.SetCustomProperties(GMTable);
        }
    }
    public List<CharacterCard> red_CharCards;
    public List<CharacterCard> blue_CharCards;
    #endregion

    #region 网络相关
    HashTable GMTable = new HashTable();

    public override void OnPlayerPropertiesUpdate(Photon.Realtime.Player targetPlayer, HashTable changedProps)
    {
        if (targetPlayer != PhotonNetwork.LocalPlayer)
            pool_CharCards = (List<CharacterCard>)changedProps["Pool_CharCards"];
    }

    #endregion

}
